package fr.inserm.umr915.vcfserver.bio.ucsc.pg;

import fr.inserm.umr915.vcfserver.bio.Chromosomes;
import fr.inserm.umr915.vcfserver.xml.XmlStreamSerializer;

public  class PersonalGenome {
	
	  private String chrom;
	  private int chromStart;
	  private int chromEnd;
	  private String name;
	  private int alleleCount;
	  private String alleleFreq;
	  private String alleleScores;
	  protected PersonalGenome()
	     {
		 
	     }
	  
	  public String getChrom()
	     {
	     return this.chrom;
	     }
	  public int getChromStart()
	     {
	     return this.chromStart;
	     }
	  public int getChromEnd()
	     {
	     return this.chromEnd;
	     }
	  public String getName()
	     {
	     return this.name;
	     }
	  public int getAlleleCount()
	     {
	     return this.alleleCount;
	     }
	  public String getAlleleFreq()
	     {
	     return this.alleleFreq;
	     }
	  public String getAlleleScores()
	     {
	     return this.alleleScores;
	     }
	@Override
	  public String toString()
	     {
	     return this.getClass().toString() +   " 'chrom'= " + this.chrom +  " 'chromStart'= " + this.chromStart +  " 'chromEnd'= " + this.chromEnd +  " 'name'= " + this.name +  " 'alleleCount'= " + this.alleleCount +  " 'alleleFreq'= " + this.alleleFreq +  " 'alleleScores'= " + this.alleleScores;
	     }
	/** XMLStreamWriter serialisation */
	  public static class StaxSerializer
	  	implements XmlStreamSerializer<PersonalGenome>
	     {
	     private String genome;
	     public StaxSerializer(String genome)
	    	 {
	    	 this.genome=genome;
	    	 }
	     @Override
	     public void write(PersonalGenome o,javax.xml.stream.XMLStreamWriter w) throws javax.xml.stream.XMLStreamException
	           {
			   w.writeStartElement(this.genome);
			  
			   w.writeStartElement("chrom");
			   w.writeCharacters(String.valueOf(o.getChrom()));
			   w.writeEndElement();
			   w.writeStartElement("chromStart");
			   w.writeCharacters(String.valueOf(o.getChromStart()));
			   w.writeEndElement();
			   w.writeStartElement("chromEnd");
			   w.writeCharacters(String.valueOf(o.getChromEnd()));
			   w.writeEndElement();
			   w.writeStartElement("name");
			   w.writeCharacters(String.valueOf(o.getName()));
			   w.writeEndElement();
			   w.writeStartElement("alleleCount");
			   w.writeCharacters(String.valueOf(o.getAlleleCount()));
			   w.writeEndElement();
			   w.writeStartElement("alleleFreq");
			   w.writeCharacters(String.valueOf(o.getAlleleFreq()));
			   w.writeEndElement();
			   w.writeStartElement("alleleScores");
			   w.writeCharacters(String.valueOf(o.getAlleleScores()));
			   w.writeEndElement();
			   w.writeEndElement();
			   }
	     }
	
     public static PersonalGenome parse(String tokens[])
        {
    	PersonalGenome o=new PersonalGenome();
        //o.bin=Short.parseShort(tokens[0]);
        o.chrom=tokens[1];
        o.chromStart=Integer.parseInt(tokens[2]);
        o.chromEnd=Integer.parseInt(tokens[3]);
        o.name=tokens[4];
        o.alleleCount=Integer.parseInt(tokens[5]);
        o.alleleFreq=tokens[6];
        o.alleleScores=(tokens.length>7?tokens[7]:"");
        return o;
        }
	     
	  public static class Binding extends com.sleepycat.bind.tuple.TupleBinding<PersonalGenome>
	     {
		 private Chromosomes chromosomes;
		 
		 public Binding(Chromosomes chromosomes)
		 	{
			this.chromosomes=chromosomes;
		 	}
		 @Override
	     public PersonalGenome entryToObject(com.sleepycat.bind.tuple.TupleInput in)
	        {
	    	PersonalGenome o=new PersonalGenome();
			o.chrom=this.chromosomes.getNameById(in.readByte());
			o.chromStart=in.readInt();
			o.chromEnd=in.readInt();
			o.name=in.readString();
			o.alleleCount=in.readByte();
			o.alleleFreq=in.readString();
			o.alleleScores=in.readString();
	        return o;
	        }
		 @Override
	     public void objectToEntry(PersonalGenome o,com.sleepycat.bind.tuple.TupleOutput out)
	        {
	    	out.writeByte(this.chromosomes.getIdByName(o.chrom));
			out.writeInt(o.chromStart);
			out.writeInt(o.chromEnd);
			out.writeString(o.name);
			out.writeByte((byte)o.alleleCount);
			out.writeString(o.alleleFreq);
			out.writeString(o.alleleScores);
	        }
	     }

}
